package com.example.administrator.daydayappmarket.game.fragment.fragments.toplist.fragment.fragments.single.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.game.fragment.fragments.choiceness.activity.GameChoiceListViewSecondActivity;
import com.example.administrator.daydayappmarket.game.fragment.fragments.toplist.fragment.fragments.single.model.GameTopListSingleInfo;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 郎志奇 on 2016/12/28.
 */

public class GameTopListSingleAdapter extends BaseAdapter {


    private List<GameTopListSingleInfo> data;

    private Context context;

    public GameTopListSingleAdapter(List<GameTopListSingleInfo> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public GameTopListSingleInfo getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.item_game_toplist_single_listview, parent, false);

            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //设置数据
        holder.mItemFragmentGameTopListListNumber.setText((i + 4) + "");
        holder.mItemFragmentGameTopListListTextName.setText(
                data.get(i).getName()
        );

        holder.mItemFragmentGameTopListListDownloadsCountsTextSize.setText(
                Integer.parseInt(data.get(i).getDownload_count()) / 10000 + "万人下载   " +
                        Integer.parseInt(data.get(i).getPackage_size()) / (1024 * 1024) + "MB"
        );

        holder.mItemFragmentGameTopListListTextReviews.setText(
                data.get(i).getReviews()
        );

        if(data.get(i).getIcon_url().startsWith("http")){

            Picasso.with(context).load(data.get(i).getIcon_url())
                    .into(holder.mItemFragmentGameTopListListImage);
        }else{
        Picasso.with(context).load(Contacts.IMAGE_PREFIX + data.get(i).getIcon_url())
                .into(holder.mItemFragmentGameTopListListImage);}

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(context, GameChoiceListViewSecondActivity.class);

                intent.putExtra("id",data.get(i).getId());

                context.startActivity(intent);
            }
        });


        return convertView;
    }

    static class ViewHolder {

        @InjectView(R.id.item_fragment_game_topList_list_linearLayout)
        LinearLayout linearLayout;

        @InjectView(R.id.item_fragment_game_topList_list_number)
        TextView mItemFragmentGameTopListListNumber;
        @InjectView(R.id.item_fragment_game_topList_list_image)
        ImageView mItemFragmentGameTopListListImage;
        @InjectView(R.id.item_fragment_game_topList_list_text_name)
        TextView mItemFragmentGameTopListListTextName;
        @InjectView(R.id.item_fragment_game_topList_list_downloads_counts_text_size)
        TextView mItemFragmentGameTopListListDownloadsCountsTextSize;
        @InjectView(R.id.item_fragment_game_topList_list_text_reviews)
        TextView mItemFragmentGameTopListListTextReviews;
        @InjectView(R.id.item_fragment_game_topList_list_download_button)
        Button mItemFragmentGameTopListListDownloadButton;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
